window.F2xExtend=__extends;
var  _height = window.innerHeight / (window.innerWidth / 640);
_height = _height <= 1040 ? 1040 : _height;
var dictHeight = 1280 - _height > 0 ? 1280 - _height : 0;
dictHeight = -dictHeight/2;
var a1 = new Audio();
var layer  = null;
a1.src= cdnUrl+'img/bg.mp3';
a1.loop = true;

window.addEventListener("load",function(){
    annie.debug=false;
    /**
     * 最上层div的id,可以在一个页面同时放多个stage.
     * 设计尺寸的宽
     * 设计尺寸的高
     * FPS刷新率
     * 缩放模式
     * 渲染模式
     */
    var stage=new annie.Stage("annieEngine",800,_height,30,annie.StageScaleMode.FIXED_HEIGHT,0);
    //默认关闭自动旋转和自动resize
    // stage.autoResize=true;
    // stage.autoSteering=true;
    	var loader = new App.Loading();
		loader.init({
			searchImgs:{
				dom:document.querySelector('.loading')
			},
		    enterCallback: function(per) {
		        console.log(per)
		    },
		    callback: function() {
		    	$(".loading,.music_box").removeClass('hide');
		    	loadFlash();
		    	a1.autoPlay();
		    }
		});
		function loadFlash(){
			stage.addEventListener(annie.Event.INIT_TO_STAGE,function (e) {
	        Flash2x.loadScene("home",function(per){
	            //加载进度
	            //trace("加载进度:"+per+"%");
	            if(per>20){
	            	var line =(100- per)+"%";
	            	$(".loading .ico_left img").css("left",line);
	            	$(".loading .ico_right img").css("right",line)
	            }
	            if(per>30){
	            	$(".loading p").html(per+"%")
	            	$(".loading p").show();
	            }
	        },function(result){
	            //加载完成 result 里包含了当前加载完成的是哪个场景序号，以及总加载场景数有多少，所以
	            //需要同时加载多个模块时可以判断已经加载好的后直接出内容，其他偷偷在后台加载
	            if(result.sceneId==result.sceneTotal) {
	            	
	            	layer = new home.Home();
	            	layer.y=dictHeight;
	                stage.addChild(layer);
	                $(".loading").fadeOut();
					$("#name").show();
					$("#name p,.nickname span").text(_nickname)
					console.log(layer)
					layer.addFrameScript(30,function(){
						$("#name").on("touchend",function(){
							$("#name").hide();
							layer.gotoAndStop(245);
							console.log("ok")
							$('.btn_rule').removeClass("hide");
						})
					})
					layer.addFrameScript(151,function(){
						$("#name").fadeOut(100);
						$('.btn_rule').removeClass("hide");
					})
	                layer.btn_go.addEventListener(annie.MouseEvent.MOUSE_DOWN,function(){
	                	$(".wraper").show();
						$(".wraper").addClass("active")
						$('.page1').hide();
						$('.page2').fadeIn();
						$('.btn_rule').addClass("hide");
						timer = setInterval(function(){
							$(".pop_notice").fadeOut();
						},3000);
					});	
					var loader = new App.Loading();
					loader.init({
					    enterCallback: function() {
					        var per = this.count / this.length * 100 >> 0;
					       // console.log(per)
					    },
					    callback: function() {
					    	init();
					    }
					});
					_sdi.stat({ptype:'8',stype:'1'}); 
	            }
	        },cdnUrl+'js/');
	    })
		}
    
});


function init(){
	var canvas  = document.createElement("canvas");
	var ctx = canvas.getContext('2d');
	canvas.width = 766;
	canvas.height = 1245;

	var changeType = 0;
	var html='',html2='';
	var curData=[];
	var nextQuestion = 0;
	var answer = timer = null;
	var pass = -2;
	var isCreate = false;
	var answerData1 =[
		['next2','next3','answer1'],
		['answer2','next4','answer3'],
		['answer4','next5'],
		['answer5','answer6','next5'],
		['answer7','next6','next7'],
		['next8','answer8','answer9'],
		['next9','answer10','answer11'],
		['next10','answer12','next9'],
		['next11','answer13','next12'],
		['next12','answer14','next9'],
		['answer15','answer16'],
		['answer15','answer17']
	];
	var link1 = [
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100698403',//人性的弱点
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100837818',//南方有乔木
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100752521',//麻衣神算子
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100804558',//东宫
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100825576',//十年一品温如言
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100825576',//十年一品温如言
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100835200',//顾盼生辉
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100542995',//豪门绝恋
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100837818',//南方有乔木
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100542995',//豪门绝恋
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100835200',//顾盼生辉
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100804558',//东宫
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100933334',//龙族
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100933334',//龙族
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100917522',//香密沉沉
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100804558',//东宫
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100164525'//何以生
		
		
//		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100147318',//红年代橙
		
	];
	var link2 = [
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100698403',//人性的弱点
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100542995',//豪门绝恋
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100804558',//东宫
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100837818',//南方有乔木
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100825576',//十年一品温如言
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100825576',//十年一品温如言
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100804558',//东宫
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100752521',//麻衣神算子
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100835200',//顾盼生辉
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100933334',//龙族
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100542995',//豪门绝恋
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100164525',//何以生
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100752521',//麻衣神算子
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100835200',//顾盼生辉
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100164525',//何以生
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100917522',//香密沉沉
		'https://bookshelf.html5.qq.com/?t=native&ch=004544&no_web_bar=0#!/catalog/1100837818'//南方有乔木
	];
	var answerData2 =[
		['answer1','next2','next3'],
		['answer2','next4','answer3'],
		['next5','next9','answer4'],
		['answer5','answer6','next6'],
		['answer7','next7','next8'],
		['next3','next10','answer8'],
		['answer9','answer10'],
		['answer11','next9','answer12'],
		['answer13','next11'],
		['next9','answer14','answer15'],
		['answer16','answer17']	
	]
	function createList(){
		var html='',html2='';
		if(changeType==0){
			curData = answerData1;
		}else{
			curData = answerData2;
		}
		for(var i=0;i<curData.length;i++){
			var src3 = cdnUrl+'img/'+(changeType+1)+'/q'+(i+1)+'.png';
			console.log(src3)
			if(i==0){
				html += '<ul>'	;
				html2+='<img src='+src3+' class="ico w100"/>'
			}else{
				html += '<ul class="hide">';
				html2+='<img src='+src3+' class="ico w100 hide"/>'
			}
			
			
			for(var j=0;j<curData[i].length;j++){
				var src1 = cdnUrl+'img/'+(changeType+1)+'/'+(i+1)+'/'+(j+1)+'.png';
				var src2 = cdnUrl+'img/'+(changeType+1)+'/'+(i+1)+'/'+(j+1)+'_2.png';
				html+='<li><img src='+src1+' class="ico"/><img src='+src1+' class="ico hide"/></li>'
			}
			html+='</ul>'
		}
		$(".answer_box").append(html);
		$('.question_box').append(html2)
	}
	window.history.replaceState('index', 'index', '#page=index');
	$(".answer_box").on("click",'li',function(){
		var index = $(this).index()
		var str = curData[$(this).parent().index()][index];
		$(this).addClass('active').siblings().removeClass('active');
		console.log(str)
		if(pass==1){
			return;
		}
		if(str.indexOf('next')>-1){
			nextQuestion = parseInt(str.slice(4)) ;
			
			pass  = 0
		}else if(str.indexOf('answer')>-1){
			answer = parseInt(str.slice(6)) ;
			pass  = 1;
		}
		
		if(pass){
			console.log("结果页")
			console.log('answer:'+answer)
			var src1 = cdnUrl+'img/'+(changeType+1)+'/type'+answer+'.jpg?version='+version;
			var src2 = cdnUrl+'img/'+(changeType+1)+'/code'+answer+'.jpg?version='+version;
			var loadImg = new Image();
			var resultImg= new Image();
			resultImg.onload = function(){
				$('#result1').attr('src',src1);
				$('.page3').hide();
				$('.page4').fadeIn();	
				$(".page4").addClass('active')
			}
			resultImg.src=src1;
			loadImg.crossOrigin = '*';
			loadImg.onload =function(){
				ctx.clearRect(0,0,this.width,this.height)
				ctx.drawImage(this,0,0,this.width,this.height);
				ctx.textAlign="center";
				ctx.font="44px Arial";
				ctx.fillStyle='#ffffff';
				ctx.fillText(_nickname,this.width/2,673);
				var result  = canvas.toDataURL()
				$('#result2').attr("src",result)
			}
			loadImg.src = src2;
			_sdi.stat({ptype:'4',stype:'1'}); 
			_sdi.stat({ptype:'8',stype:'3'});
		}else{
			console.log('nextQuestion:'+nextQuestion);
			setTimeout(function(){
				$(".question_box img").eq(nextQuestion-1).removeClass('hide').siblings().addClass('hide');
				$(".answer_box ul").eq(nextQuestion-1).removeClass('hide').siblings().addClass('hide');
				pass = -2
			},300)
			
		}
	});
    window.onpopstate = function ( e ) {
      var currentState = history.state;
      if ( e.state == 'index' ) {
       	location.reload()
      }
    };
	$(".page1").on('click',function(){
		$(".wraper").addClass("active")
		$('.page1').hide();
		$('.page2').fadeIn();
		$('.btn_rule').addClass("hide");
		timer = setInterval(function(){
			$(".pop_notice").fadeOut();
		},3000);
	});
	$(".btn_rule").on("click",function(){
		console.log('go')
		$(this).addClass("hide");
		$("#annieEngine").show();
		$(".wraper,.page1").fadeIn();
		$(".page1").addClass("active");
		_sdi.stat({ptype:'7',stype:'12'}); 
		_sdi.stat({ptype:'8',stype:'2'});
		_sdi.stat({ptype:'9',stype:'1'});
	});
	$(".btn_start,#scroll .tem_box img").on('click',function(){
		createList();
		$('.page2').hide();
		$('.page3').fadeIn();
		_sdi.stat({ptype:'7',stype:'13'}); 
	});
	$(".pop_notice").on("click",function(){
		$(".pop_notice").fadeOut();
		clearInterval(timer);
	});
	$(".btn_link").on("click",function(){
		var temLink  = null;
		console.log(answer-1)
		if(changeType==0){
			 temLink = link1[answer-1];
			 
			 switch(answer-1){
				case 0:
				_sdi.stat({ptype:'7',stype:'1'}); 
				break;
				case 1:
				_sdi.stat({ptype:'7',stype:'2'}); 
				break;
				case 2:
				_sdi.stat({ptype:'7',stype:'3'}); 
				break;
				case 3:
				_sdi.stat({ptype:'7',stype:'4'}); 
				break;
				case 4:
				_sdi.stat({ptype:'7',stype:'6'}); 
				break;
				case 5:
				_sdi.stat({ptype:'7',stype:'11'}); 
				break;
				case 6:
				_sdi.stat({ptype:'7',stype:'7'}); 
				break;
				case 7:
				_sdi.stat({ptype:'7',stype:'5'}); 
				break;
				case 8:
				_sdi.stat({ptype:'7',stype:'9'}); 
				break;
				case 9:
				_sdi.stat({ptype:'7',stype:'10'}); 
				break;
			}
		}else{
			temLink = link2[answer-1];
			switch(answer-1){
				case 0:
				_sdi.stat({ptype:'7',stype:'1'}); 
				break;
				case 1:
				_sdi.stat({ptype:'7',stype:'2'}); 
				break;
				case 2:
				_sdi.stat({ptype:'7',stype:'3'}); 
				break;
				case 3:
				_sdi.stat({ptype:'7',stype:'4'}); 
				break;
				case 4:
				_sdi.stat({ptype:'7',stype:'5'}); 
				break;
				case 5:
				_sdi.stat({ptype:'7',stype:'6'}); 
				break;
				case 6:
				_sdi.stat({ptype:'7',stype:'7'}); 
				break;
				case 7:
				_sdi.stat({ptype:'7',stype:'8'}); 
				break;
				case 8:
				_sdi.stat({ptype:'7',stype:'9'}); 
				break;
				case 9:
				_sdi.stat({ptype:'7',stype:'10'}); 
				break;
			}
		}
		
		
		location.href = temLink;
	});
	$(".btn_share").on("click",function(){
		$('.page5').fadeIn();
		$(".music_box").addClass("hide");
		_sdi.stat({ptype:'7',stype:'1'}); 
	});
	$(".page5").on("click",function(){
		$('.page5').fadeOut();
		$(".music_box").removeClass("hide");
	});
	$(".music_box").on("touchend",function(){
		if(!$(this).hasClass("off")){
			$(this).addClass("off");
			a1.pause();
		}else{
			$(this).removeClass("off");
			a1.play();
		}
	});
	$(".btn_to").on("click",function(){
		if(pass==-2){
			alert("请选择答案")
			return;
		}
		if(pass){
			console.log("结果页")
			console.log('answer:'+answer)
			var src1 = cdnUrl+'img/'+(changeType+1)+'/type'+answer+'.jpg';
			var src2 = cdnUrl+'img/'+(changeType+1)+'/code'+answer+'.jpg';
			var img1 = new Image();
			
			img1.src = src1;
			$('#result1').attr('src',src1);
			$('#result2').attr('src',src2)
			$('.page3').hide();
			$('.page4').fadeIn();
		}else{
			console.log('nextQuestion:'+nextQuestion+1);
			$(".question_box img").eq(nextQuestion).removeClass('hide').siblings().addClass('hide');
			$(".answer_box ul").eq(nextQuestion).removeClass('hide').siblings().addClass('hide');
			pass = -2
		}
		
	});
	$(".btn_two").on('click',function(){
		$('#annieEngine').removeClass("hide");
		$('.page4 ,.wraper').fadeOut();
		$('.wraper').removeClass('active');
		pass = -2;
		$('.btn_rule').removeClass("hide");
		$(".page3 li,.page1,.page4").removeClass("active");
		
		$(".answer_box ul").remove();
		$('.question_box img').remove();
		_sdi.stat({ptype:'7',stype:'15'}); 
	});
  	var swipe = new App.Swipe();
  	swipe.init({
  		dom : document.querySelector('.page2 .bd_box'),
  		callback : function(direction){
  			if(direction=='left'){
  				 slide.next();
  				 _sdi.stat({ptype:'7',stype:'14'});
  			}else if(direction=='right'){
  				slide.prev()
  			}
  		}
  	});
  	var slide  = (function(){
  		var $box = $('.page2 .tem_box img'),
  			l = $('.page2 .tem_box img').length,
  			$prev = $('.page2 .btn_prev'),
  			$next = $('.page2 .btn_next'),
  			curIndex = 0;
  		$prev.on("click",function(){
  			prev();
  		});
  		$next.on("click",function(){
  			next();
  		});
  		function next(){
  			if(curIndex<l-1){
  				curIndex++
  			}
  			if(curIndex==l-1){
  				$next.addClass('hide');
  				$prev.removeClass('hide');
  			}
  			changeType = curIndex;
  			$box.eq(curIndex).fadeIn().siblings().fadeOut();
  		}
  		
  		function prev(){
  			if(curIndex>0){
  				curIndex--
  			}
  			if(curIndex==0){
  				$prev.addClass('hide');
  				$next.removeClass('hide');
  			}
  			changeType = curIndex;
  			$box.eq(curIndex).fadeIn().siblings().fadeOut();	
  		}
  		return {
  			next:next,
  			prev:prev
  		}
  	})();
 
}
